<?php

class InfoSongAddAction extends BaseAction 
{
	/**
	 * @maxLength 255
	 * @label Song Title (Chinese)
	 */
	public $title_chinese;
	/**
	 * @maxLength 255
	 * @label Song Title (English)
	 */
	public $title_english;
	public $genre;
	public $singers;
	public $group_names;
	public $producers;
	public $composers;
	public $lyricists;
	public $arrangers;
	public $record_company;
	/**
	 * @filter positive_number
	 * @maxLength 11
	 * @label Year
	 */
	public $year;
	public $lyrics;
	public $owners;
	
	/**
	 * @filter datetime
	 * @label Publish Date/Time
	 */
    public $publish_datetime;
	
	/**
	 * @filter datetime
	 * @label Expire Date/Time
	 */
    public $expire_datetime;
	
	public $status;
	public $created_by;
	public $created_at;
	public $modified_by;
	public $modified_at;

	public $programme_id;
	public $onair_episode_no;

	public $programme_title_cn;
	public $programme_title_en;
	public $programme_path;

	#public $role_id;
	public $cast_id; # modify by vanni,for bug 7397

	public $name_type;
	public $name_last;
	public $name_first;
	public $alphabet;

	public $description;

	public $ut;
	public $reset;
	public function execute(){
		if (!is_null($this->reset))
		{
			$this->setData('type', 'song');
			$this->setData('act', 'add');
			return 'reset';
		}

		Model::$dbo['w']->beginTransaction();
		try
		{
			if(count($this->_errors) > 0){
				$this->init_page_data();
				return 'filter-error';
			}

			//check name unique
			if (is_null($this->title_chinese) && is_null($this->title_english))
			{
				$this->init_page_data();
				$this->addError('Song.Song Title', array('0038', 'Song Title'));
				return 'filter-error';
			}

			//check programme and on air episode no
			$check_exists = episode::getEpisodeID($this->programme_id, $this->onair_episode_no);
			if ($check_exists == '')
			{
				$this->addError('Programme or Episode', array('0103', 'Programme or Episode'));
				$this->init_page_data();
				return 'logic-error';
			}

            //check publish_datetime && expire_datetime
            if (empty($this->publish_datetime)
                || empty($this->expire_datetime)
                || $this->publish_datetime > $this->expire_datetime) {

                $this->init_page_data();
                $this->addError('Publish Date/Time && Expire Date/Time', array('0033', 'Publish Date/Time && Expire Date/Time'));
                return 'filter-error';
            }

			$insert_id = $this->add_info_song();
			if (FALSE != $insert_id)
			{
				$this->setData('type', 'song');
				$this->setData('onair_episode_no', $this->onair_episode_no);
				$this->setData('programme_id', $this->programme_id);
			}

			Model::$dbo['w']->commit();

			return "succ";
		}
		catch(Exception $e)
		{
			error_log("Add Song Fails: " . var_export($this, 1));
			error_log("Exception: " . var_export($e, 1));

			Model::$dbo['w']->rollBack();
			return 'system-error';
		}
	}    

	/**
	 * add cast base information
	 */
	private function add_info_song()
	{
		$info_song = new info_song();
		$info_song->set($this);
		$info_song->created_at = date("Y-m-d H:i:s", $_SERVER['REQUEST_TIME']);
		$info_song->created_by = CommonUtils::get_user();
        $info_song->modified_at = $info_song->created_at;
        $info_song->modified_by = $info_song->created_by;
		$info_song->status = AC::STATUS_NEW;
		$info_song->episode_id = episode::getEpisodeID($this->programme_id, $this->onair_episode_no);
		$info_song->singers = CommonUtils::json_info_data($this, 'singers');
		$info_song->group_names = CommonUtils::json_info_data($this, 'group_names');
		$info_song->producers = CommonUtils::json_info_data($this, 'producers');
		$info_song->composers = CommonUtils::json_info_data($this, 'composers');
		$info_song->lyricists = CommonUtils::json_info_data($this, 'lyricists');
		$info_song->arrangers = CommonUtils::json_info_data($this, 'arrangers');
		$info_song->owners = CommonUtils::json_info_data($this, 'owners');
		$info_song->id = $insert_id = Model::$dbo['w']->save($info_song);

		if ($insert_id > 0)
		{
			$info_song->add_change_log();

            $primary_key = array(
                'id'    => $insert_id
            );
            $fields = array(
                'title_chinese',
                'title_english',
                'singers',
                'group_names',
                'producers',
                'composers',
                'lyricists',
                'arrangers',
                'record_company',
                'lyrics',
                'owners',
                'description'
            );
            CommonUtils::filterC2A0('info_song', $primary_key, $fields);

			$this->addInfo("info_song:".AC::LOG_OP_CREATE.":".$insert_id, "add info_song success", json_encode($info_song));
			return $insert_id;
		}
		else
		{
			$this->addError("info_song:".AC::LOG_OP_CREATE.":".$insert_id, "add info_song error", json_encode($info_song));
			return false;
		}

	}

	/**
	 * init page data
	 */
	private function init_page_data()
	{
		$song = new info_song();
		$ref = new ReflectionObject($this);
		$props = $ref->getProperties();
		foreach ($props as $prop)
		{
			if ($prop->isPublic())
			{
				$prop_name = $prop->getName();
				$song->$prop_name = $this->$prop_name;
			}
		}
		$this->setData('song', $song);
		$this->setData('publishLog', '');

		$json_fields = AC::$infoJson['info_song'];
		$json_data = array();
		foreach ($json_fields as $field)
		{
			$json_decode_data = json_decode(CommonUtils::json_info_data($this, $field), true);
			$json_data[$field] = isset($json_decode_data[$field]) ? $json_decode_data[$field] : array();
		}
		$this->setData("song_json_list", $json_data);

		//jump page
		$this->setData('type', 'song');
		$this->setData('act', 'add');
		$this->setData('step', 1);
	}
}
?>
